package com.eastfair.es;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.user.LoginUser;
import com.eastfair.annotation.log.entity.OptLogDTO;
import com.eastfair.auth.dto.UserAccountDto;
import com.eastfair.auth.vo.UserAccountVo;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.elasticsearch.model.PageResult;
import com.eastfair.es.dao.PointLogDao;
import com.eastfair.es.dao.StudentDao;
import com.eastfair.es.dto.PointLogDto;
import com.eastfair.es.entity.PointLog;
import com.eastfair.es.entity.Student;
import com.eastfair.es.service.PointLogService;
import com.eastfair.es.vo.PointLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;


/**
 * <p>
 * 前端控制器
 * 用户账户信息
 * </p>
 *
 * @author ligang
 * @date 2021-08-10
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/pointlog")
@Api(value = "es", tags = "用户账户信息")
public class PointLogController {

    @Autowired
    PointLogDao pointLogDao;

    @Autowired
    PointLogService pointLogService;

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ResponseBody
    public R findById(String id) {
        PointLog pointLog = (PointLog) pointLogDao.get(id, PointLog.class);
        return R.success(pointLogService.assembleLog(pointLog));
    }


    /**
     * 模糊查询
     *
     * @return
     */
    @RequestMapping(value = "/querylistPage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "模糊查询", notes = "模糊查询")
//    public R regexpQuery(@RequestBody PageParams<PointLogDto> params, @LoginUser(isFull = true) UserAccountVo userAccountVo) {
    public R<PageResult<PointLogVo>> regexpQuery(@RequestBody PageParams<PointLogDto> params) {
        PointLogDto pointLogDto = params.getModel();
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(pointLogDto));
        lowerJsonObject(jsonObject);
        List<String> noLikeParams = Arrays.asList("applicaiontName", "pointName", "port", "tarce", "userId", "userName", "ip", "codeNumber");
        PageResult<PointLog> pointLogPageResult = pointLogDao.querylistPage((int) params.getCurrent(), (int) params.getSize(), jsonObject, PointLog.class, noLikeParams);
        PageResult<PointLogVo> result = new PageResult();
        BeanUtil.copyProperties(pointLogPageResult, result, new String[]{"records"});
        result.setRecords(pointLogService.assembleLogList(pointLogPageResult.getRecords()));
        return R.success(result);
    }


    public void lowerJsonObject(JSONObject jsonObject){
        for(String key : jsonObject.keySet()){
            Object value = jsonObject.get(key);
            if(value.getClass().getName().equals(String.class.getName()) && value!=null){
                jsonObject.put(key,value.toString().toLowerCase());
            }
        }
    }

//    /**
//     * 高亮查询
//     *
//     * @param key
//     * @param value
//     * @return
//     */
//    @RequestMapping(value = "/highlightQuery", method = RequestMethod.GET)
//    @ResponseBody
//    public R highlightQuery(Integer pageNum, Integer pageSize, String key, String value) {
//        PageResult<Student> studentPageResult = studentDao.highlightBuilder(pageNum, pageSize,
//                key, "<font color='red'>", "</font>", value, Student.class);
//        log.info(JSON.toJSONString(studentPageResult));
//        return R.success(studentPageResult);
//    }


//    /**
//     * anc查询
//     *
//     * @return
//     */
//    @RequestMapping(value = "/boolQueryBuilderByMust", method = RequestMethod.GET)
//    @ResponseBody
//    public R boolQueryBuilderByMust( String name, String address) {
//        Map<String, String[]> fields = new HashMap<>();
//        fields.put("sName",new String[]{name});
//        fields.put("sAddress",new String[]{address});
//        List<Student> studentPageResult = studentDao.boolQueryBuilderByMust(fields, Student.class);
//        log.info(JSON.toJSONString(studentPageResult));
//        return R.success(studentPageResult);
//    }


}
